import logging
import time
import unittest
import random
import requests

from api.loginAPI import loginAPI
from utils import assert_utils, read_imgVerify_data, read_registr_data, read_param_data
from parameterized import parameterized


class login(unittest.TestCase):
    phone1 = '17688880012'
    phone2 = '17688880023'
    phone3 = '17688880034'
    phone4 = '17688880045'
    phone5 = '17688880056'
    imgCode = '8888'
    pwd = "test123"

    def setUp(self) -> None:
        self.login_api = loginAPI()
        self.session = requests.Session()

    def tearDown(self) -> None:
        self.session.close()

    # @parameterized.expand(read_imgVerify_data("../data/imgVerify.json"))
    @parameterized.expand(read_param_data("../data/imgVerify.json","test_get_img_verify","type,status_code"))
    def test01_get_img_code(self, type, status_code):
        # 根据不同type类型准备不同的参数数据
        r = ''
        if type == 'float':
            r = str(random.random())
        elif type == 'int':
            r = str(random.randint(10000000, 99999999))
        elif type == 'char':
            r = ''.join(random.sample("abcdefghijklmn", 8))

        # 发送请求
        response = self.login_api.getImgCode(self.session, r)
        logging.info("r = {} response = {}".format(r, response))
        # 断言
        self.assertEqual(status_code, response.status_code)

    """
    # 参数为随机小数时获取图片验证码成功
    def test01_get_img_code_random_float(self):
        # 定义参数
        r = random.random()
        # 调用接口类中的接口
        response = self.login_api.getImgCode(self.session, str(r))
        # 接收返回结果，断言
        self.assertEqual(200, response.status_code)

    # 参数为随机整数时获取图片验证码成功
    def test02_get_img_code_random_int(self):
        r = random.randint(10000000, 90000000)
        response = self.login_api.getImgCode(self.session, str(r))
        self.assertEqual(200, response.status_code)

    # 参数为空时获取图片验证码失败
    def test03_get_img_code_null(self):
        response = self.login_api.getImgCode(self.session, "")
        self.assertEqual(404, response.status_code)

    # 参数为字母时获取图片验证码失败
    def test04_get_img_code_char(self):
        r = random.sample("abcdefghijklmn", 8)
        rand = ''.join(r)
        response = self.login_api.getImgCode(self.session, rand)
        self.assertEqual(400, response.status_code)
    """

    # 参数正确,获取短信验证码成功
    def test05_get_SmsCode_fail(self):
        # 获取图片验证码
        r = random.random()
        response = self.login_api.getImgCode(self.session, str(r))
        self.assertEqual(200, response.status_code)

        response = self.login_api.getSmsCode(self.session, self.phone1, self.imgCode)
        logging.info("get sms code response = {}".format(response.json()))
        assert_utils(self, response, 200, 200, "短信发送成功")

    # 图片验证码错误,获取短信验证码失败
    def test06_get_wrong_SmsCode_fail(self):
        # 获取图片验证码
        r = random.random()
        response = self.login_api.getImgCode(self.session, str(r))
        self.assertEqual(200, response.status_code)

        response = self.login_api.getSmsCode(self.session, self.phone1, "6666")
        logging.info("get sms code response = {}".format(response.json()))
        assert_utils(self, response, 200, 100, "图片验证码错误")

    # 图片验证码为空,获取短信验证码失败
    def test07_get_SmsCode_is_null_fail(self):
        # 获取图片验证码
        r = random.random()
        response = self.login_api.getImgCode(self.session, str(r))
        self.assertEqual(200, response.status_code)

        response = self.login_api.getSmsCode(self.session, self.phone1, "")
        logging.info("get sms code response = {}".format(response.json()))
        assert_utils(self, response, 200, 100, "图片验证码错误")

    # 未调用获取图片验证码接口，直接获取短信验证码失败
    def test08_not_getSmsCode_fail(self):
        response = self.login_api.getSmsCode(self.session, self.phone1, self.imgCode)
        logging.info("get sms code response = {}".format(response.json()))
        assert_utils(self, response, 200, 100, "图片验证码错误")

    @parameterized.expand(read_registr_data("../data/register.json"))
    # @parameterized.expand(read_param_data("../data/register.json","test_register","phone, pwd, invite_phone, imgCode, SmsCode, dyServe, status_code, status, description"))
    def test09_register(self, phone, pwd, invite_phone, imgCode, SmsCode, dyServe, status_code, status, description):
        # 获取图片验证码
        r = random.random()
        response = self.login_api.getImgCode(self.session, str(r))
        self.assertEqual(200, response.status_code)
        # 获取短信验证码
        response = self.login_api.getSmsCode(self.session, phone, self.imgCode)
        logging.info("get sms code response = {}".format(response.json()))
        assert_utils(self, response, 200, 200, "短信发送成功")
        # 参数化注册
        response = self.login_api.register(self.session, phone, pwd, invite_phone, imgCode, SmsCode, dyServe)
        logging.info("register response = {} --- phone = {} imgCode={} SmsCode={} ".format(response.json(),phone,imgCode,SmsCode))
        # 断言
        assert_utils(self, response, status_code, status, description)

    """
    # 输入必填项注册成功
    def test09_register_success_param_required(self):
        # 获取图片验证码
        r = random.random()
        response = self.login_api.getImgCode(self.session, str(r))
        self.assertEqual(200, response.status_code)
        # 获取短信验证码
        response = self.login_api.getSmsCode(self.session, self.phone1, self.imgCode)
        logging.info("get sms code response = {}".format(response.json()))
        assert_utils(self, response, 200, 200, "短信发送成功")
        # 注册
        response = self.login_api.register(self.session, self.phone1, self.pwd, '')
        logging.info("register response = {}".format(response.json()))
        assert_utils(self, response, 200, 200, "注册成功")

    # 输入所有输入项注册成功
    def test10_register_success_param_all(self):
        # 获取图片验证码
        r = random.random()
        response = self.login_api.getImgCode(self.session, str(r))
        self.assertEqual(200, response.status_code)
        # 获取短信验证码
        response = self.login_api.getSmsCode(self.session, self.phone2, self.imgCode)
        logging.info("get sms code response = {}".format(response.json()))
        assert_utils(self, response, 200, 200, "短信发送成功")
        # 注册
        response = self.login_api.register(self.session, self.phone2, self.pwd, invite_phone='18888889595')
        logging.info("register response = {}".format(response.json()))
        assert_utils(self, response, 200, 200, "注册成功")

    # 图片验证码错误，其他参数正确，注册失败
    def test11_register_fail_imgCode_error(self):
        # 获取图片验证码
        r = random.random()
        response = self.login_api.getImgCode(self.session, str(r))
        self.assertEqual(200, response.status_code)
        # 获取短信验证码
        response = self.login_api.getSmsCode(self.session, self.phone3, self.imgCode)
        logging.info("get sms code response = {}".format(response.json()))
        assert_utils(self, response, 200, 200, "短信发送成功")
        # 注册
        response = self.login_api.register(self.session, self.phone3, self.pwd, imgCode="2222", invite_phone='')
        logging.info("register response = {}".format(response.json()))
        assert_utils(self, response, 200, 100, "验证码错误!")

    # 手机号已存在，其他参数正确，注册失败
    def test12_register_fail_phone_exist(self):
        # 获取图片验证码
        r = random.random()
        response = self.login_api.getImgCode(self.session, str(r))
        self.assertEqual(200, response.status_code)
        # 获取短信验证码
        response = self.login_api.getSmsCode(self.session, self.phone2, self.imgCode)
        logging.info("get sms code response = {}".format(response.json()))
        assert_utils(self, response, 200, 200, "短信发送成功")
        # 注册
        response = self.login_api.register(self.session, self.phone2, self.pwd, invite_phone='')
        logging.info("register response = {}".format(response.json()))
        assert_utils(self, response, 200, 100, "手机已存在!")

    # 短信验证码错误，其他参数正确，注册失败
    def test12_register_fail_SmsCode_error(self):
        # 获取图片验证码
        r = random.random()
        response = self.login_api.getImgCode(self.session, str(r))
        self.assertEqual(200, response.status_code)
        # 获取短信验证码
        response = self.login_api.getSmsCode(self.session, self.phone4, self.imgCode)
        logging.info("get sms code response = {}".format(response.json()))
        assert_utils(self, response, 200, 200, "短信发送成功")
        # 注册
        response = self.login_api.register(self.session, self.phone4, self.pwd, SmsCode="111", invite_phone='')
        logging.info("register response = {}".format(response.json()))
        assert_utils(self, response, 200, 100, "验证码错误")

    # 密码为空，其他参数正确，注册失败
    def test13_register_fail_pwd_null(self):
        # 获取图片验证码
        r = random.random()
        response = self.login_api.getImgCode(self.session, str(r))
        self.assertEqual(200, response.status_code)
        # 获取短信验证码
        response = self.login_api.getSmsCode(self.session, self.phone4, self.imgCode)
        logging.info("get sms code response = {}".format(response.json()))
        assert_utils(self, response, 200, 200, "短信发送成功")
        # 注册
        response = self.login_api.register(self.session, self.phone4, pwd='', invite_phone='')
        logging.info("register response = {}".format(response.json()))
        assert_utils(self, response, 200, 100, "密码不能为空")

    # 不同意条款，其他参数正确，注册失败
    def test14_register_fail_dy_serve_off(self):
        # 获取图片验证码
        r = random.random()
        response = self.login_api.getImgCode(self.session, str(r))
        self.assertEqual(200, response.status_code)
        # 获取短信验证码
        response = self.login_api.getSmsCode(self.session, self.phone5, self.imgCode)
        logging.info("get sms code response = {}".format(response.json()))
        assert_utils(self, response, 200, 200, "短信发送成功")
        # 注册
        response = self.login_api.register(self.session, self.phone5, self.pwd, invite_phone='', dyServer="off")
        logging.info("register response = {}".format(response.json()))
        assert_utils(self, response, 200, 100, "请同意我们的条款")
    """

    # 手机号、密码正确，登录成功
    def test15_all_right_login_success(self):
        response = self.login_api.login(self.session, self.phone1)
        logging.info("login response = {}".format(response.json()))
        assert_utils(self, response, 200, 200, "登录成功")

    # 手机号未注册，密码正确，登陆失败
    def test16_phone_no_register_login_fail(self):
        response = self.login_api.login(self.session, self.phone3)
        logging.info("login response = {}".format(response.json()))
        assert_utils(self, response, 200, 100, "用户不存在")

    # 手机号正确，密码为空，登陆失败
    def test17_pwd_null_login_fail(self):
        response = self.login_api.login(self.session, self.phone1, password='')
        logging.info("login response = {}".format(response.json()))
        assert_utils(self, response, 200, 100, "密码不能为空")

    # 手机号正确，密码错误，登陆失败
    def test18_pwd_error_1_login_fail(self):
        response = self.login_api.login(self.session, self.phone2, password='123132')
        logging.info("login response = {}".format(response.json()))
        assert_utils(self, response, 200, 100, "密码错误1次,达到3次将锁定账户")
        response = self.login_api.login(self.session, self.phone2, password='123132')
        logging.info("login response = {}".format(response.json()))
        assert_utils(self, response, 200, 100, "密码错误2次,达到3次将锁定账户")
        response = self.login_api.login(self.session, self.phone2, password='123132')
        logging.info("login response = {}".format(response.json()))
        assert_utils(self, response, 200, 100, "由于连续输入错误密码达到上限，账号已被锁定，请于1.0分钟后重新登录")
        response = self.login_api.login(self.session, self.phone2)
        logging.info("login response = {}".format(response.json()))
        assert_utils(self, response, 200, 100, "由于连续输入错误密码达到上限，账号已被锁定，请于1.0分钟后重新登录")
        time.sleep(60)
        response = self.login_api.login(self.session, self.phone2)
        logging.info("login response = {}".format(response.json()))
        assert_utils(self, response, 200, 200, "登录成功")
